package com.polar.rs200;

import java.io.File;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
//import android.app.AlertDialog;


public class rs200Activity extends Activity {
	 // On charge la librairie
 	static {
        System.loadLibrary("DSPLib");
    }  
		 
    // On déclare la fonction "native"
   // private native String invokeNativeFunction();
	 private native int byteDecode(int restart, int sample);
	// private TextView lblInfo;
	// static TextView lblStatus;
	 
	 private recording recorderInstance;
	 
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
         
        Button cmdAction = (Button) this.findViewById(R.id.cmdStart);
        final TextView lblInfo = (TextView) this.findViewById(R.id.lblInfo);
        
        // Record 20 seconds of audio.
        recorderInstance = new recording();

        cmdAction.setOnClickListener(new View.OnClickListener() {
             @Override
     		public void onClick(View v) {
                 // Perform action on click
             //int test = byteDecode(1,20);
            	// On "appelle" notre fonction
             //String hello = invokeNativeFunction();
            int value = byteDecode(1,10);
              
             //On affiche le résultat
              //new AlertDialog.Builder(this).setMessage(hello).show();    
             lblInfo.setText(String.valueOf(value));
             
             //tv2.setText( "test" );
            
             Thread th = new Thread(recorderInstance);
             recorderInstance.setFileName(new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/test.raw"));
             th.start();
             recorderInstance.setRecording(true);
             
             synchronized (this) {
             try {
             this.wait(20000);
             	} catch (InterruptedException e) {
             		e.printStackTrace();
             }
             }
             
             recorderInstance.setRecording(false);
             try {
            	 th.join();
             	 } catch (InterruptedException e) {
             		 e.printStackTrace();
             	 }

             }
         });
    }
    
    
   public void UpdateStatus (String txt) {
	    final TextView lblStatus = (TextView) this.findViewById(R.id.lblUpdateStatus);
    	lblStatus.setText(txt);   	
    	    }
}